package com.chauncy.DMS.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import com.chauncy.DMS.bean.StuInfoBean;
import com.chauncy.DMS.dao.UserDaoImplement;

public class UserLoginServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		String flag = request.getParameter("logout");
		//System.out.println(flag);
		if(!request.getParameter("logout").equals("") ){
			if( flag.equals("logout")){
				logout(request, response);
				response.sendRedirect("./index.jsp");
			}
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		UserDaoImplement udi = new UserDaoImplement();
		//用户Bean类
		StuInfoBean sib = new StuInfoBean();
		//获取用户输入用户名及密码
		String stuNub = String.valueOf(request.getParameter("userName"));
		String stuPWD = String.valueOf(request.getParameter("password"));
		//赋值
		sib.setStu_number(stuNub);
		sib.setStu_pwd(stuPWD);
		StuInfoBean stuIb  = new StuInfoBean();
		List<Map<String,Object>> sDormInfoListMap = new ArrayList<Map<String,Object>>();
		//登录请求
		stuIb = udi.userLogin(sib);
		//查询该学生是否存在住宿记录
		sDormInfoListMap= udi.userInfoExist(stuNub);
		//stuid 不为空说明存在此用户
		if(stuIb != null){
			//提示前端页面用户存在
			//当用户存在住宿记录时 传递session
			if(sDormInfoListMap.size() >0){
				HttpSession httpSession = request.getSession();
				httpSession.setAttribute("sDormInfoListMap", sDormInfoListMap);
				response.getWriter().print("userDormInfoExist");
			}else{
				//不存在入住信息则传递用户信息 以便于登记入住
				HttpSession httpSession = request.getSession();
				httpSession.setAttribute("stuIb", stuIb);
				response.getWriter().print("userDormInfoNotExist");
			}	
		}else{
			//提示前端页面用户不存在
			response.getWriter().print("userNotExist");
		}
		//判断ajax第二次访问servlet提交的参数
		if(request.getParameter("userFlag") != null && !request.getParameter("userFlag").equals("")){
			int userFlag = Integer.parseInt(String.valueOf(request.getParameter("userFlag")));
			if(userFlag == 1){
				try {
					changePageToSystem(request, response);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else if(userFlag == 2){
				try {
					changePageToSelectDorm(request, response);
					//logout(request, response);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	//退出登录
	private static void logout(HttpServletRequest request ,
			HttpServletResponse response){
		request.getSession().invalidate();
	}
	//跳转到系统主页
	private static void changePageToSystem(HttpServletRequest request ,
			HttpServletResponse response)
			throws Exception{
			request.getRequestDispatcher("./html/stu_func_page.jsp").forward(request, response);	
	}
	//跳转到宿舍选择页面
	private static void changePageToSelectDorm(HttpServletRequest request ,
			HttpServletResponse response)
			throws Exception{
			response.sendRedirect("./select_dorm.jsp");
	}
}
